package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.T;
import com.epfresh.bean.NewHomeBean;

import java.util.ArrayList;
import java.util.List;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2019/4/23
 * Desc:
 */
public class BaseSpellGoodsAdapter extends ItemViewBinder<NewHomeBean.SpellsBean,BaseSpellGoodsAdapter.ViewHolder> {
//    private List<NewHomeBean.SpellsBean> goods = new ArrayList<>();
    private OnGoodItemClickListener onGoodItemClickListener;

    public void setOnGoodItemClickListener(OnGoodItemClickListener onGoodItemClickListener) {
        this.onGoodItemClickListener = onGoodItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_spell_single_good, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final NewHomeBean.SpellsBean item) {
        ImageLoader.getInstance().displayImage(item.getThumbnail(),holder.img_goods);
        holder.txt_good_name.setText(item.getTitle());
        if (item.getSpecName() != null && !item.getSpecName().equals("默认")) {
            holder.txt_pack.setText(item.getSpecName() + "(" + item.getPack() + ")");
        } else {
            holder.txt_pack.setText(item.getPack());
        }
        holder.txt_price.setText("¥" + item.getPrice() + "/" + item.getChargeUnit());
        holder.txt_time_end.setText(item.getSaleTime());
        holder.txt_time_delivery.setText(item.getDeliveryTime());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onGoodItemClickListener != null) {
                    onGoodItemClickListener.onGoodItemClick(item);
                }
            }
        });
    }

//    @NonNull
//    @Override
//    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_base_spell_single_good, parent, false);
//        return new ViewHolder(itemView);
//    }
//
//    @Override
//    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
//        ImageLoader.getInstance().displayImage(goods.get(position).getThumbnail(),holder.img_goods);
//        holder.txt_good_name.setText(goods.get(position).getTitle());
//        if (goods.get(position).getSpecName() != null && !goods.get(position).getSpecName().equals("默认")) {
//            holder.txt_pack.setText(goods.get(position).getSpecName() + "(" + goods.get(position).getPack() + ")");
//        } else {
//            holder.txt_pack.setText(goods.get(position).getPack());
//        }
//        holder.txt_price.setText("¥" + goods.get(position).getPrice() + "/" + goods.get(position).getChargeUnit());
//        holder.txt_time_end.setText(goods.get(position).getSaleTime());
//        holder.txt_time_delivery.setText(goods.get(position).getDeliveryTime());
//
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onGoodItemClickListener != null) {
//                    onGoodItemClickListener.onGoodItemClick(goods.get(holder.getAdapterPosition()));
//                }
//            }
//        });
//    }
//
//    @Override
//    public int getItemCount() {
//        return goods.size();
//    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_goods;
        private TextView txt_good_name,txt_pack,txt_price,txt_time_end,txt_time_delivery,txt_shop;

        ViewHolder(View itemView) {
            super(itemView);
            img_goods = itemView.findViewById(R.id.img_goods);
            txt_good_name = itemView.findViewById(R.id.txt_good_name);
            txt_pack = itemView.findViewById(R.id.txt_pack);
            txt_price = itemView.findViewById(R.id.txt_price);
            txt_time_end = itemView.findViewById(R.id.txt_time_end);
            txt_time_delivery = itemView.findViewById(R.id.txt_time_delivery);
            txt_shop = itemView.findViewById(R.id.txt_shop);
        }
    }

//    public void setGoods(List<NewHomeBean.SpellsBean> goods) {
//        this.goods.clear();
//        this.goods.addAll(goods);
//        getAdapter().notifyDataSetChanged();
//    }

    public interface OnGoodItemClickListener {
        void onGoodItemClick(NewHomeBean.SpellsBean spellsBean);
    }


}
